/*
 * ClassLoaderOIS.java
 *
 * Version:
 *      $Id$
 *
 * Copyright (c) 2009 Peter O. Erickson
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without major
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, and/or distribute copies of, but
 * under no circumstances sublicense and/or sell, the Software,
 * and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package edu.rit.poe.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;

/**
 * An implementation of <tt>ObjectInputStream</tt>, to allow a custom
 * <tt>ClassLoader</tt> for finding the <tt>Class</tt> of serialized objects.
 * 
 * @author  Peter O. Erickson
 *
 * @version $Id$
 */
public class ClassLoaderOIS extends ObjectInputStream {
   
    /** The custom class loader of this <tt>ObjectInputStream</tt>. */
   private ClassLoader cl;
   
   /**
    * Constructs a new <tt>ClassLoaderOIS</tt> with the specified
    * <tt>ClassLoader</tt>, and wrapping the specified <tt>InputStream</tt>.
    * 
    * @param    is              the <tt>InputStream</tt> to be wrapped
    * @param    cl              the <tt>ClassLoader</tt> to use to find the
    *                           <tt>Class</tt> the serialized objects that are
    *                           read in from the stream
    * 
    * @throws   IOException     
    */
   public ClassLoaderOIS( InputStream is, ClassLoader cl)
           throws IOException {
      super( is );
      this.cl = cl;
   }
   
   /**
    * Load the local class equivalent of the specified stream class description.
    * This subclass of <tt>ObjectInputStream</tt> will use the
    * <tt>ClassLoader</tt> that was specified at construction.
    * 
    * @param    desc                    an instance of the class
    *                                   <tt>ObjectStreamClass</tt>
    * 
    * @return                           a <tt>Class</tt> object corresponding to
    *                                   <tt>desc</tt>
    * 
    * @throws   IOException             any of the usual Input/Output exceptions
    * @throws   ClassNotFoundException  if class of a serialized object cannot
    *                                   be found
    */
   @Override
   protected Class<?> resolveClass( ObjectStreamClass desc ) throws IOException,
           ClassNotFoundException {
      return Class.forName( desc.getName(), false, cl );
   }
    
} // ClassLoaderOIS
